﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Antu.WebControls;
using Antu.Core.Components;
using Antu.Core.Services;
using System.Web.UI.WebControls;
using EluZSProvider;
using System.Web;
using System.Data;

namespace WebSys.UI.Control.ZhengShou
{
    public class EditRoomControl : MPBasicTemplate
    {
        EluZSDataProvider _EluZSDataProvider = (EluZSDataProvider)ProviderService.ProviderServiceMange.GetProvider("EluZSProvider");
        public string userid = string.Empty;
        protected override void OnLoad(EventArgs e)
        {
            if (HttpContext.Current.Session["UserID"] == null)
            {
                Antu.Utilities.UtilitiesPage.AlertDialogClear("登录超时，请重新登录！", "window.parent.location.href('../login.aspx');");
            }
            else
            {
                userid = HttpContext.Current.Session["UserID"].ToString();
            }
            base.OnLoad(e);
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            Panel panel1 = (Panel)FindControl("panel1");
            if (!base.Page.IsPostBack)
            {
                BindDDl();
                Persistence1.Table = "ZS_PROLOUPAN";
                Persistence1.Key = "ID";
                Persistence1.KeyValue = HttpContext.Current.Request["id"];
                Persistence1.ConnStr = _EluZSDataProvider.GetConnectionString();

            }
            Persistence1.BindControl = panel1;
            BindData();
            
        }
        protected override void OnPreRender(EventArgs e) {
            base.OnPreRender(e);
        }
        protected void BindData() {
            Button btnSave = (Button)FindControl("btnSave");
            Button btnReturn = (Button)FindControl("btnReturn");
            Button btnQKXY = (Button)FindControl("btnQKXY");
            btnSave.Click += new EventHandler(btnSave_Click);
            btnReturn.Click += new EventHandler(btnReturn_Click);
            btnQKXY.Click+=new EventHandler(btnQKXY_Click);
            TextBox 产权人 = (TextBox)FindControl("产权人");
            TextBox 承租人 = (TextBox)FindControl("承租人");
            EluZSProvider.ProLoupan proloupan = _EluZSDataProvider.GetProLoupanRoom(HttpContext.Current.Request["ID"]);
            string strCZR = _EluZSDataProvider.GetSomeStr("zs_zsperson", "承租人", "id", proloupan.Zspid);
            string strBSW = _EluZSDataProvider.GetSomeStr("zs_cqzhxy", "bsw", "fid", proloupan.Id);
            string strCQR = _EluZSDataProvider.GetSomeStr("zs_zsperson", "产权人", "id", proloupan.Zspid);
            承租人.Text = strCZR;
            产权人.Text = strCQR;
            if (!string.IsNullOrEmpty(HttpContext.Current.Request["QUERY"]))
            {
                if (HttpContext.Current.Request["QUERY"] == "loupan")
                {
                    btnQKXY.Visible = false;
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "showw", "<script>showw();</script>");
                }
                else
                {
                    if (!string.IsNullOrEmpty(strCZR))
                    {
                        Page.ClientScript.RegisterStartupScript(this.GetType(), "dipsaytr", "<script>dipsaytr();</script>");
                    }
                }
            }
            if (strBSW == "0")
            {
                产权人.Enabled = false;
                承租人.Enabled = false;
                btnQKXY.Text = "已签协议";
                Page.ClientScript.RegisterStartupScript(this.GetType(), "displaycc", "<script>displaycc();</script>");
            }
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {
            MPPersistence Persistence1 = (MPPersistence)FindControl("Persistence1");
            Persistence1.Update();
            HttpContext.Current.Response.Redirect("EditRoom.aspx?ID=" + Persistence1.KeyValue + "&PID=" + HttpContext.Current.Request["PID"] + "&ZID=" + HttpContext.Current.Request["ZID"] + "&query=" + HttpContext.Current.Request["query"]);
        }
        protected void btnReturn_Click(object sender, EventArgs e)
        {
            HttpContext.Current.Response.Redirect("ProLoupan.aspx?PID=" + HttpContext.Current.Request["PID"] + "&ZID=" + HttpContext.Current.Request["ZID"] + "&query=" + HttpContext.Current.Request["query"]);
        }
        protected void btnQKXY_Click(object sender, EventArgs e)
        {
            string pID = HttpContext.Current.Request["PID"];
            string zID = HttpContext.Current.Request["ZID"];
            string fID = HttpContext.Current.Request["ID"];
            EluZSProvider.ProLoupan proloupan = _EluZSDataProvider.GetProLoupanRoom(HttpContext.Current.Request["ID"]);
            string temp = proloupan.楼层房间号;

            string qsid = _EluZSDataProvider.GetSomeStr("zs_cqzhxy", "id", "fid", fID);
            string getsession = _EluZSDataProvider.GetSomeStr("ZS_USER", "TABFS", "ID", userid);
            if (getsession == "0")
            {
                if (qsid != "")
                {
                    HttpContext.Current.Response.Redirect("ChanQuanZhiHuanXieYiQianShu.aspx?PID=" + HttpContext.Current.Request["PID"].ToString() + "&ZID=" + HttpContext.Current.Request["ZID"].ToString() + "&ID=" + qsid + "&FID=" + fID + "&action=edit");
                }
                else
                {
                    HttpContext.Current.Response.Redirect("ChanQuanZhiHuanXieYiQianShu.aspx?PID=" + HttpContext.Current.Request["PID"].ToString() + "&ZID=" + HttpContext.Current.Request["ZID"].ToString() + "&FID=" + fID + "&action=insert");
                }
            }
            else if (getsession == "1")
            {
                if (qsid != "")
                {
                    HttpContext.Current.Response.Redirect("ChanQuanZhiHuanXieYiQianShuInOne.aspx?PID=" + HttpContext.Current.Request["PID"].ToString() + "&ZID=" + HttpContext.Current.Request["ZID"].ToString() + "&ID=" + qsid + "&FID=" + fID + "&action=edit");
                }
                else
                {
                    HttpContext.Current.Response.Redirect("ChanQuanZhiHuanXieYiQianShuInOne.aspx?PID=" + HttpContext.Current.Request["PID"].ToString() + "&ZID=" + HttpContext.Current.Request["ZID"].ToString() + "&FID=" + fID + "&action=insert");
                }
            }
        }

        public void BindDDl()
        {
            DropDownList ddl房屋类型 = (DropDownList)FindControl("ddl房屋类型");
            List<EluZSProvider.Params> _params = _EluZSDataProvider.GetParams("幢房屋类型");
            ddl房屋类型.DataSource = _params;
            ddl房屋类型.DataTextField = "keyname";
            ddl房屋类型.DataValueField = "keyname";
            ddl房屋类型.DataBind();

            DropDownList ddl房型 = (DropDownList)FindControl("ddl房型");
            List<EluZSProvider.Params> _params2 = _EluZSDataProvider.GetParams("房型");
            ddl房型.DataSource = _params2;
            ddl房型.DataTextField = "keyname";
            ddl房型.DataValueField = "keyname";
            ddl房型.DataBind();
            
        }
    }
}


